package com.oleg.bigidoc.beans;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import com.oleg.bigidoc.db.Connection;
import com.oleg.bigidoc.domain.Document;
import com.oleg.bigidoc.domain.Employee;
import com.oleg.bigidoc.domain.Organization;
import com.oleg.bigidoc.domain.Regemp;

@SuppressWarnings("serial")
@ManagedBean(name = "registrationBean")
@SessionScoped
public class RegistrationBean implements Serializable{
	
	private Connection conn = new Connection();
	private EntityManager em;
	private Query query;
	
	private String lastName;
	private String firstName;
	private String middleName;
//	private Organization organization;
	private String officer;
	private String phone;
	private String attestat;
	private String address;
	private String email;
//	private Document document;
	private String login;
	private String password;
	
	@ManagedProperty(value="#{navigationBean}")
    private NavigationBean navigationBean;	
	
	public String doSave() {         
        // Set logout message
		
		em = conn.getConnection();
		query = em.createNativeQuery("SELECT * FROM REGEMP r WHERE r.LOGIN = ?", Regemp.class);
		query.setParameter(1, login);
		List<Regemp> result = query.getResultList();
		if(!result.isEmpty()){
			FacesMessage msg = new FacesMessage("Такой пользователь уже существует!", "ERROR MSG");
	        msg.setSeverity(FacesMessage.SEVERITY_ERROR);
	        FacesContext.getCurrentInstance().addMessage(null, msg);
	         
	        return navigationBean.toLogin();
		}
		Regemp reg = new Regemp(login, password, false);
		query = em.createNativeQuery("INSERT INTO REGEMP (login, password, is_active) VALUES(?,?,?)");
		query.setParameter(1, reg.getLogin());
		query.setParameter(2, reg.getPassword());
		query.setParameter(3, reg.isIs_active());
		EntityTransaction tx = em.getTransaction();
		tx.begin();
		query.executeUpdate();
		tx.commit();	
		
		conn.Close();
	
		FacesMessage msg = new FacesMessage("Пользователь зарегистрирован!", "ERROR MSG");
        msg.setSeverity(FacesMessage.SEVERITY_ERROR);
        FacesContext.getCurrentInstance().addMessage(null, msg);
         
        return navigationBean.toLogin();
    }
	
    public void setNavigationBean(NavigationBean navigationBean) {
        this.navigationBean = navigationBean;
    }
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getMiddleName() {
		return middleName;
	}
	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}
	public String getOfficer() {
		return officer;
	}
	public void setOfficer(String officer) {
		this.officer = officer;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getAttestat() {
		return attestat;
	}
	public void setAttestat(String attestat) {
		this.attestat = attestat;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}
